package com.smforj.ssm.modules.api.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smforj.ssm.frame.core.enums.RouterMapping;
import com.smforj.ssm.frame.core.utils.StringEx;
import com.smforj.ssm.frame.core.web.controller.BaseControllerSupportImpl;
import com.smforj.ssm.frame.core.web.domain.DataResult;
import com.smforj.ssm.modules.lib.domain.CheckInResponse;
import com.smforj.ssm.modules.lib.domain.CheckOutResponse;
import com.smforj.ssm.modules.lib.domain.ItemResponse;
import com.smforj.ssm.modules.lib.domain.LibraryRequest;
import com.smforj.ssm.modules.lib.domain.PatronResponse;
import com.smforj.ssm.modules.lib.domain.SysBook;
import com.smforj.ssm.modules.lib.domain.SysConfig;
import com.smforj.ssm.modules.lib.domain.SysReader;
import com.smforj.ssm.modules.lib.domain.vo.SysReaderVo;
import com.smforj.ssm.modules.lib.service.LibraryService;
import com.smforj.ssm.modules.lib.service.ReaderWechatService;
import com.smforj.ssm.modules.sys.service.SysReaderService;
import com.smforj.ssm.modules.wc.service.BusinessService;
import com.smforj.ssm.util.PropKit;

/***
 * 图书馆业务操作Controller 
 * 该Controler需要Interrupter进行Token拦截
 *
 *
 *
 * @author Haijun Gao 
 * @date 2016-8-11 下午2:00:54
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */
@RouterMapping("") 
@RequestMapping("/library/api")
public class LibraryController extends BaseControllerSupportImpl {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private ReaderWechatService readerWechatService;
    @Autowired
    @Qualifier("sysReaderService")
    private SysReaderService sysReaderService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private LibraryService libraryService;


    /****
     * 客户端与服务器登陆接口
     * @param userVo
     * @return
     * @date 2017-6-30 下午2:26:29
     */
    @RequestMapping(value = {"/login"}, method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<PatronResponse> login(@RequestBody LibraryRequest<String> request) {
        DataResult<PatronResponse> result = new DataResult<PatronResponse>();
        SysReader query = new SysReader();
        query.setRfidCardNum(request.getSzUserId());
        if (StringEx.isExEmpty(request.getSzUserPWD()))
            query.setPassword(request.getSzUserPWD());
        SysReaderVo rv = (SysReaderVo) sysReaderService.queryOne(query);
        //前期为显示信息，后期将删除
        if (rv != null) {
            request.setSzUserId(rv.getBarcode());
            PatronResponse patron = libraryService.getReader(request);
            patron.setRfid(rv.getRfidCardNum());
            patron.setCardid(rv.getIdCardNum());
            patron.setCampusCardNum(rv.getBarcode());
            result.addOK("", patron);
        } else
            result.addError("用户不合法");
        return result;
    }

    /***
     * 查询图书信息
     * @return
     * @date 2016-11-1 下午4:13:57
     */
    @RequestMapping(value = {"/book/{id}"}, method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<ItemResponse> getBook(@PathVariable("id") String id) {
        DataResult<ItemResponse> result = new DataResult<ItemResponse>();
        try {
            SysBook book = businessService.getBookInfo(id);
            if (book != null) {
                LibraryRequest<String> request = new LibraryRequest<String>(); 
                request.setData(book.getBarcode());
                ItemResponse item = libraryService.getBookInfo(request);
                item.setBarcode(book.getBarcode());
                item.setRfid(book.getRfid());

                result.addOK("", item);
            } else
                result.addError("无图书信息" + id);
        } catch (Exception ex) {
            log.error("查询图书出现异常：" + ex.getMessage());
            result.addError("获取用户权限失败: " + ex.getMessage());
        }
        return result;
    }

    /***
     * 借阅
     * @return
     * @date 2016-8-11 下午2:03:45
     */
    @RequestMapping(value = {"/checkout"}, method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<List<CheckOutResponse>> doCheckout(@RequestBody LibraryRequest<String[]> request) {
        System.out.println("BusinessController doCheckout");
        DataResult<List<CheckOutResponse>> result = new DataResult<List<CheckOutResponse>>();
        try {
            result.addOK("", libraryService.doCheckout(request));
        } catch (Exception ex) {
            log.error("借阅失败: " + ex.getMessage());
            result.addError("借阅异常: " + ex.getMessage());
        }
        return result;
    }

    /***
     * 借阅
     * @return
     * @date 2016-8-11 下午2:03:45
     */
    @RequestMapping(value = {"/checkin"}, method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<List<CheckInResponse>> doCheckin(@RequestBody LibraryRequest<String[]> request) {
        System.out.println("BusinessController doCheckout");
        DataResult<List<CheckInResponse>> result = new DataResult<List<CheckInResponse>>();
        try {
            result.addOK("归还成功", libraryService.doCheckin(request));
        } catch (Exception ex) {
            log.error("归还失败: " + ex.getMessage());
            result.addError("归还失败: " + ex.getMessage());
        }
        return result;
    }

    /***
     * 续借
     * @return
     * @date 2016-8-11 下午2:03:45
     */
    @RequestMapping(value = {"/renew"}, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<List<CheckOutResponse>> doRenew(@RequestBody LibraryRequest<String[]> request) {
        System.out.println("BusinessController doRenew");
        DataResult<List<CheckOutResponse>> result = new DataResult<List<CheckOutResponse>>();
        try {
            result.addOK("", libraryService.doRenew(request));
        } catch (Exception ex) {
            log.error("续借失败: " + ex.getMessage());
            result.addError("续借失败: " + ex.getMessage());
        }
        return result;
    }

    /***
     * 查询图书信息
     * @return
     * @date 2016-11-1 下午4:13:57
     */
    @RequestMapping(value = {"/config/{id}"}, method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<SysConfig> getSysConfig(@PathVariable("id") String id) {
        DataResult<SysConfig> result = new DataResult<SysConfig>();
        SysConfig sc = new SysConfig();
        sc.setSip2ip(PropKit.get("sip2ip"));
        sc.setSip2port(Integer.parseInt(PropKit.get("sip2port")));
        result.addOK("", sc);
        return result;
    }


    /**
     * 暂用
     *
     * @param msg 参数
     */
    @RequestMapping(value = {"/test/{msg}"}, method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String sendAndRead(@PathVariable("msg") String msg) {
        return libraryService.testSocket(msg);
    }


}
